namespace Xmef.Engine
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Xmef.Interfaces;
    using Xmef.Library;

    [Serializable]
    public class GenericCollection<T> : IContainer<T>, IGenericCollection<T>
    {
        private List<T> _list = new List<T>();

        public GenericCollection()
        {
            _list = new List<T>();
        }

        public GenericCollection(IEnumerable<T> collection) 
        {
            _list = new List<T>(collection);
        }

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return this._list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._list.GetEnumerator();
        }

        #endregion

        #region IContainer<T> Members

        public bool Add(T key)
        {
            if (!this._list.Contains(key))
            {
                this._list.Add(key);
                return true;
            }
            return false;
        }

        public bool Contains(T value)
        {
            return this._list.Contains(value);
        }

        public bool Remove(T key)
        {
            if (this._list.Contains(key))
            {
                this._list.Remove(key);
                return true;
            }
            return false;
        }

        public int Count
        {
            get
            {
                return _list.Count;
            }
        }

        #endregion

        public void Clear()
        {
            this._list.Clear();
        }

        protected IEnumerable<T> List
        {
            get 
            {
                return _list;
            }
        }

        public T this[int index]
        {
            get 
            {
                if (index < 0 || index > _list.Count) return default(T);
                return _list[index];
            }
            protected set 
            {
                if (index < 0 || index > _list.Count) return;
                _list[index] = value;
            }
        }
    }
}

